const wrongNameList = [];

function getFilename(path) {
  const arrayName = path.match(/[^./]+/g);
  return arrayName[arrayName.length - 2];
}

// 检查公共组件文件名
const componentsFiles = require.context("@/components", true, /.+\.vue$/);
componentsFiles.keys().forEach((modulePath) => {
  if (!/^([A-Z][a-z\d]+)+$/.test(getFilename(modulePath))) {
    wrongNameList.push(modulePath.replace(/^\./, "src/components"));
  }
});

// 检查页面组件文件名
const viewsFiles = require.context("@/views", true, /.+\.vue$/);
viewsFiles.keys().forEach((modulePath) => {
  if (!/^([A-Z][a-z\d]+)+$/.test(getFilename(modulePath))) {
    wrongNameList.push(modulePath.replace(/^\./, "src/views"));
  }
});

// 检查assets中图片的文件名
const assetsFiles = require.context("@/assets", true, /.+\.(a?png|jpe?g|bmp|webp|gif)$/);
assetsFiles.keys().forEach((modulePath) => {
  if (/^[^a-z\d_]+$/.test(getFilename(modulePath))) {
    wrongNameList.push(modulePath.replace(/^\./, "src/assets"));
  }
});

if (wrongNameList.length > 0) {
  console.log("请修改不符合规范的文件名 ↓");
  console.table(wrongNameList.map((name) => ({ filename: name })));
}
